package com.gxa.financial.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.common.R;
import com.gxa.common.entity.financial_management.WithdrawalSetting;
import com.gxa.financial.mapper.WithdrawalSettingMapper;
import com.gxa.financial.service.WithdrawalSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class WithdrawalSettingServiceImpl extends ServiceImpl<WithdrawalSettingMapper, WithdrawalSetting> implements WithdrawalSettingService {

    @Autowired
    private WithdrawalSettingMapper withdrawalSettingMapper;

    @Override
    public R addWithdrawalSetting(WithdrawalSetting withdrawalSetting) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("account_type",withdrawalSetting.getAccountType());
        WithdrawalSetting withdrawalSetting1 = withdrawalSettingMapper.selectOne(qw);
        R r = new R();
        if (withdrawalSetting1 != null) {//不等于空说明有这个数据，执行修改
            withdrawalSetting.setId(withdrawalSetting1.getId());
            int i = withdrawalSettingMapper.updateById(withdrawalSetting);
            if (i > 0) {
                r.setCode(200);
                r.setMessage("设置成功");
            }else {
                r.setCode(500);
                r.setMessage("设置失败");
            }
        }else {//执行添加
            int insert = withdrawalSettingMapper.insert(withdrawalSetting);
            if (insert > 0) {
                r.setCode(200);
                r.setMessage("添加成功");
            }else {
                r.setCode(500);
                r.setMessage("添加失败");
            }
        }
        return r;
    }

    @Override
    public R selectWithdrawalSetting(Byte accountType) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("account_type",accountType);
        WithdrawalSetting withdrawalSetting = withdrawalSettingMapper.selectOne(qw);
        R r = new R();
        if(withdrawalSetting != null){
            r.setCode(200);
            r.setMessage("查询成功");
            r.setData(withdrawalSetting);
        }else {
            r.setCode(500);
            r.setMessage("查询失败或没有数据");
        }
        return r;
    }

}
